﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Collections;
using Kwan.Core;
// 开源协议:Apache License, Version 2.0
// Copyright © FanJianHan 2008
// mail:henryfan@msn.com
namespace Kwan.Data
{

  

   

 
    /// <summary>
   
    /// MSSQL数据库
    /// </summary>
    public class MSSQL : IDriver {
        #region IDriver 成员

        public IDbConnection Connection {
            get { return new SqlConnection(); }
        }

        public IDbDataAdapter DataAdapter(IDbCommand cmd) {
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = (SqlCommand)cmd;
            return da;
        }

        public IDbCommand Command {
            get {
                return new SqlCommand();
            }
        }

        public string ReplaceSql(String sql) {
            return sql.Replace("@", "@");
        }

        public IDataParameter CreateParameter(string name, object value, ParameterDirection direction) {
            SqlParameter sp = new SqlParameter("@" + name, value);
            if (value == null)
                sp.Value = DBNull.Value;
            sp.Direction = direction;
            return sp;
        }

        #endregion
    }
    /// <summary>
   
    /// ACCESS数据库
    /// </summary>
    public class MSAccess : IDriver {
        #region IDriver 成员

        public IDbConnection Connection {
            get { return new OleDbConnection(); }
        }

        public IDbDataAdapter DataAdapter(IDbCommand cmd) {
            OleDbDataAdapter da = new OleDbDataAdapter();
            da.SelectCommand = (OleDbCommand)cmd;
            return da;
        }

        public IDbCommand Command {
            get {
                return new OleDbCommand();
            }
        }

        public string ReplaceSql(String sql) {
            return sql.Replace("@", "@");
        }

        public IDataParameter CreateParameter(string name, object value, ParameterDirection direction) {
            OleDbParameter sp = new OleDbParameter("@" + name, value);
            sp.Direction = direction;
            if (value == null)
                sp.Value = DBNull.Value;
            else if (value.GetType() == typeof(DateTime))
            {
                sp.Value = value.ToString();
            }
            else
            {
                sp.Value = value;
            }
            return sp;
        }

        #endregion
    }
    /// <summary>
  
    /// ODBC数据库
    /// </summary>
    public class ODBC : IDriver {
        #region IDriver 成员

        public IDbConnection Connection {
            get { return new OdbcConnection(); }
        }

        public IDbDataAdapter DataAdapter(IDbCommand cmd) {
            OdbcDataAdapter da = new OdbcDataAdapter();
            da.SelectCommand = (OdbcCommand)cmd;
            return da;
        }

        public IDbCommand Command {
            get { return new OdbcCommand(); }
        }

        public string ReplaceSql(string sql) {
            return Regex.Replace(sql, "@[a-zA-z_0-9]+", "?", RegexOptions.IgnoreCase);
        }

        public IDataParameter CreateParameter(string name, object value, ParameterDirection direction) {
            OdbcParameter op = new OdbcParameter();
            op.ParameterName = "?";
            op.Value = value;
            if (value == null)
                op.Value = DBNull.Value;
            op.Direction = direction;
            return op;

        }

        #endregion
    }
    /// <summary>
   
    /// ORACLE数据库
    /// </summary>
   /* public class Oracle : IDriver {
        #region IDriver 成员

        public IDbConnection Connection {
            get { return new OracleConnection(); }
        }

        public IDbDataAdapter DataAdapter(IDbCommand cmd) {
            OracleDataAdapter da = new OracleDataAdapter();
            da.SelectCommand =(OracleCommand)cmd;
            return da;
        }

        public IDbCommand Command {
            get { return new OracleCommand(); }
        }

        public string ReplaceSql(string sql) {
            return sql.Replace("@", ":"); ;
        }

        public IDataParameter CreateParameter(string name, object value, ParameterDirection direction) {
            OracleParameter op = new OracleParameter();
            op.ParameterName = ":" + name;
            op.Value = value;
            if (value == null)
                op.Value = DBNull.Value;
            op.Direction = direction;
            return op;
        }

        #endregion
    }
   */

 
    /// <summary>
  
    /// 命令执行描述
    /// </summary>
    public interface ICommandExecute {
        int Execute(IConnectinContext cc);
    }
    /// <summary>
   
    /// 添加数据命令
    /// </summary>
    public class Insert:ICommandExecute {

        private Command mCommand;
        public Insert(string table) {
          
            mCommand = new Command("Insert into " + table);
        }
        private IList<Field> mInserFields = new List<Field>();
        public void AddField(string name, object value) {
            AddField(name, value, true);
        }
        public void AddField(string name, object value, bool isparameter) {
            Field f = new Field();
            f.IsParameter = isparameter;
            f.Name = name;
            f.Value = value;
            mInserFields.Add(f);
        }
        public int Execute(IConnectinContext cc) {
            System.Text.StringBuilder names, values;
            names = new System.Text.StringBuilder();
            values = new System.Text.StringBuilder();
            Field field;
            for (int i = 0; i < mInserFields.Count; i++) {
                if (i > 0) {
                    names.Append(",");
                    values.Append(",");
                }
                field = (Field)mInserFields[i];

                names.Append(field.Name);
                if (field.IsParameter) {
                    values.Append("@" + field.ParameterName);
                    if (field.Value != null)
                        mCommand.AddParameter(field.ParameterName, field.Value);
                    else
                        mCommand.AddParameter(field.ParameterName, DBNull.Value);
                } else
                    values.Append(field.Value);

            }
            mCommand.Text.Append("(" + names + ")" + " Values(" + values + ")");
            object value = cc.ExecuteScalar(mCommand);
            if (value == null || value == DBNull.Value)
                return 0;
            return Convert.ToInt32(value);

        }
    }
    /// <summary>
      /// 字段值描述
    /// </summary>
    public class Field {
        private object mValue;
        public object Value {
            get {
                return mValue;
            }
            set {
                mValue = value;
            }
        }
        private string mParameterName;
        public string ParameterName
        {
            get
            {
                return string.IsNullOrEmpty(mParameterName) ? Name : mParameterName;
            }
            set
            {
                mParameterName = value;
            }
        }
        private string mName;
        public string Name {
            get {
                return mName;
            }
            set {
                mName = value;
            }
        }
        private bool mIsParameter = true;
        public bool IsParameter {
            get {
                return mIsParameter;
            }
            set {
                mIsParameter = value;
            }
        }
        public string GetValueBy {
            get;
            set;
        }
        public bool GetValueAfterInsert {
            get;
            set;
        }
        
    }
   
    /// <summary>
     /// 更新数据命令
    /// </summary>
    public class Update:ICommandExecute {
        private Command mCommand;
        public Update(string table) {
          
            mCommand = new Command("Update " + table +" set ");
        }
        private IList<Field> mFields = new List<Field>();
        public void AddField(string name,string parametername, object value) {
            AddField(name,parametername, value, true);
        }
        public void AddField(string name, string parametername, object value, bool isparameter)
        {
            Field f = new Field();
            f.IsParameter = isparameter;
            f.Name = name;
            f.ParameterName = parametername;
            f.Value = value;
            mFields.Add(f);
        }
        private Expression mWhere = new Expression();
        public Expression Where
        {
            get {
                return mWhere;
            }
            set {
                mWhere = value;
            }
        }

        #region ICommandExecute 成员
        public int Execute()
        {
            using (IConnectinContext cc = DBContext.Context1)
            {
               return Execute(cc);
            }
        }
        public int Execute(IConnectinContext cc)
        {
            for (int i = 0; i < mFields.Count; i++) {
                if (i > 0)
                    mCommand.Text.Append(",");
                if (!mFields[i].IsParameter) {
                    mCommand.Text.Append(mFields[i].Name + "=" + mFields[i].Value);
                } else {
                    mCommand.Text.Append(mFields[i].Name + "=@" + mFields[i].ParameterName);
                    mCommand.AddParameter(mFields[i].ParameterName, mFields[i].Value);
                }
            }
            Where.Parse(mCommand);
            object value= cc.ExecuteScalar(mCommand);
            if (value == null || value == DBNull.Value)
                return 0;
            return Convert.ToInt32(value);
        }

        #endregion
    }
    /// <summary>
    /// 删除数据命令
    /// </summary>
    public class Delete:ICommandExecute {
        private Command mCommand;
        public Delete(string table) {
          
            mCommand = new Command("Delete from " + table);
        }
        private Expression mWhere = new Expression();
        public Expression Where
        {
            get
            {
                return mWhere;
            }
            set
            {
                mWhere = value;
            }
        }
        #region ICommandExecute 成员
        public int Execute()
        {
            using (IConnectinContext cc = DBContext.Context1)
            {
               return Execute(cc);
            }
        }
        public int Execute(IConnectinContext cc) {
            Where.Parse(mCommand);
            object value = cc.ExecuteScalar(mCommand);
            if (value == null || value == DBNull.Value)
                return 0;
            return Convert.ToInt32(value);
        }

        #endregion
    }
    /// <summary>
    /// 数据加载区间描述
    /// </summary>
    [Serializable]
    public class Region
    {
        public Region()
        {
        }
        public Region(int pageindex,int size)
        {
            Size = size;
            Start = pageindex * size;
        }
        public int Start
        {
            get;
            set;
        }
        public int Size
        {
            get;
            set;
        }
    }


   


}
